#include <iostream>
#include <string>

#include "utils/Mutex.h"
#include "db/MySqlConn.h"
#include "db/MySqlConf.h"
#include "db/MySqlConnPool.h"
#include "log/Log.h"
#include "utils/TimeUtil.h"

using namespace std;
using namespace utils;
using namespace db;

static log::Logger::ptr g_ptrLogger = GET_LOGGER_BY_NAME("test");

void test()
{
    MySqlConn con;
    bool ret = con.connect("db_test", "root", "root", "127.0.0.1", 3306);
    if (!ret)
    {
        return;
    }

    ret = con.excute("insert into tb_test(name) values(\"abc\")");

    ret = con.excute("select * from tb_test");
    if (!ret)
    {
        return;
    }

    int n = con.column();
    for (int i = 0; i < n; ++i)
    {
        cout << con.name(i) << "\t";
    }
    cout << endl;

    while (con.next())
    {
        for (int i = 0; i < n; ++i)
        {
            cout << con.value(i) << "\t";
        }
        cout << endl;
    }
}

void test2(int n)
{
    time_t t = TimeUtil::now();
#if 0
    for (int i = 0; i < n; i++)
    {
        MySqlConn con;

        bool ret = con.connect("test", "root", "root", "127.0.0.1", 3306);
        if (!ret)
        {
            return;
        }

        ret = con.excute("insert into tb_test(name) values(\"abc\")");
        if (!ret)
        {
            return;
        }

        //ret = con.excute("select * from tb_test");
        //if (!ret)
        //{
        //    return;
        //}
    }
#else

    db::MySqlConnPool::instance()->init("./config/mysql.json");
    for (int i = 0; i < n; i++)
    {
        MySqlConn::ptr ptrConn = db::MySqlConnPool::instance()->getConn();
        if (nullptr != ptrConn)
        {
            bool ret = ptrConn->excute("insert into tb_test(name) values(\"abc\")");
            if (!ret)
            {
                return;
            }
        }
    }

#endif

    LOG_INFO(g_ptrLogger) << TimeUtil::now() - t; 
}

int main(int argc, char const *argv[])
{

    //test();
    test2(10000);
    return 0;
}
